package top.as.sean.biz.system.impl;

import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.as.sean.biz.system.IRoleMenu;
import top.as.sean.dao.dto.system.RoleAuth;
import top.as.sean.dao.entity.pojo.system.RoleMenu;
import top.as.sean.dao.mapper.system.RoleMenuMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色-菜单
 *
 * @author: ArchieSean
 * @create: 2022/11/29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleMenuImpl implements IRoleMenu {
    /**
     * 角色-菜单
     */
    private final RoleMenuMapper roleMenuMapper;

    /**
     * 根据角色id查询用户id
     *
     * @param roleId 角色id
     * @return list
     */
    @Override
    public List<Long> queryMenuIdByRoleId(Long roleId) {
        return roleMenuMapper.queryMenuIdByRoleId(roleId);
    }

    /**
     * 删除授权
     *
     * @param roleId 角色id
     */
    @Override
    public void deleteByRoleId(Long roleId) {
        roleMenuMapper.deleteByRoleId(roleId);
    }

    /**
     * 新增授权
     *
     * @param dto 入参
     */
    @Override
    public void addBatch(RoleAuth dto) {
        List<RoleMenu> list = Lists.newArrayList();
        dto.getMenuIds().forEach(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(dto.getRoleId());
            roleMenu.setMenuId(menuId);
            list.add(roleMenu);
        });
        roleMenuMapper.insertBatch(list);
    }
}
